package com.opoc.service.Impl;

import com.opoc.mapper.AdminMapper;
import com.opoc.mapper.ChangeReviewMapper;
import com.opoc.mapper.EnterpriseMapper;
import com.opoc.mapper.PoolMapper;
import com.opoc.pojo.*;
import com.opoc.service.ChangeReviewService;
import com.opoc.utils.FileUtils;
import com.opoc.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;


/**
 * 变更审核
 */
@Slf4j
@Service
public class ChangeReviewImpl implements ChangeReviewService {

    @Autowired
    ChangeReviewMapper changeReviewMapper;
    @Autowired
    EnterpriseMapper enterpriseMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PoolMapper  poolMapper;


    /**
     * 5.1待审核变更请求查询--------(完成)
     */

    public List<ChangeCheck> WaitingForReview(String company_id, String company_name, String contacts) {
        List<ChangeCheck_sql> datas =  changeReviewMapper.WaitingForReview(company_id, company_name, contacts);
        List<ChangeCheck> changeRequests = new ArrayList<>();
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "企业联系人");
        map.put(2, "联系方式");
        map.put(3, "最高水位");
        map.put(4, "最低水位");
        map.put(5, "池口数量");
        map.put(6, "废水停留时间");
        map.put(7, "吃口是否在车间内");
        map.put(8, "是否安装曝气装置");
        map.put(9, "最近一次清淤时间");
        map.put(10, "当前状态");
        for (ChangeCheck_sql data : datas) {
            ChangeCheck  changecheck = new ChangeCheck();
            BeanUtils.copyProperties(data, changecheck);
            // 判断更改字段
            Integer change_property = data.getChange_property();
            System.out.println("更改字段："+change_property);
            String s = map.get(change_property);
            log.info(s);
            changecheck.setChange_property(s);

            // 格式化时间
            changecheck.setSubmit_date(TimeUtils.SqlDateToStringDate(data.getSubmit_date()));

            // 转换附件
            String urlFromPath = FileUtils.getURLFromPath(data.getEvidence_file());
            changecheck.setEvidence_file(urlFromPath);

            // 将转换后的数据添加到changeRequests集合中
            changeRequests.add(changecheck);
        }
        return changeRequests;
    }

    /**
     * 5.2已审核变更请求查询-----(完成)
     */
    @Override
    public List<ChangeRequest > ReviewedForChanges(String company_id, String company_name, String contacts) {
        List<ChangeRequest_sql> datas =  changeReviewMapper.ReviewedForChanges(company_id, company_name, contacts);
        List<ChangeRequest> changeRequests = new ArrayList<>();
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "企业联系人");
        map.put(2, "联系方式");
        map.put(3, "最高水位");
        map.put(4, "最低水位");
        map.put(5, "池口数量");
        map.put(6, "废水停留时间");
        map.put(7, "吃口是否在车间内");
        map.put(8, "是否安装曝气装置");
        map.put(9, "最近一次清淤时间");
        map.put(10, "当前状态");
        for (ChangeRequest_sql data : datas) {
            ChangeRequest changeRequest = new ChangeRequest();
            BeanUtils.copyProperties(data, changeRequest);
            //判断更改字段
            String string = map.get(data.getChange_property());
            changeRequest.setChange_properties(string);

            //审核结果
            if (data.getAuditor_result() == 1) {

                changeRequest.setRemark("错误驳回");
            } else if(data.getAuditor_result() == 0){
                changeRequest.setAuditor_result("通过");
            }

            //时间格式化
            String auditDate = TimeUtils.SqlDateToStringDate(data.getAudit_date());
            String submitDate = TimeUtils.SqlDateToStringDate(data.getSubmit_date());


            changeRequest.setAudit_date(auditDate);
            changeRequest.setSubmit_date(submitDate);

            changeRequests.add(changeRequest);
        }

        return changeRequests;


    }

    /**
     * 5.3已审核变更请求删除----(完成)
     */
    @Override
    public void Delete(List<Integer> request_ids) {

        List<String> list = new ArrayList<>();
        if (request_ids != null && !request_ids.isEmpty()) {
            // 循环遍历request_ids列表
            for (Integer request_id : request_ids) {
                // 通过request_id找到company_id
                ChangeRequest_sql massage = changeReviewMapper.selectMesage(request_id);
                String company_id = massage.getCompany_id();
                log.info("获取的request_ids：{}", company_id);
                list.add(company_id);
            }
            log.info("集合list：{}", list);
            changeReviewMapper.deleteByList(list);
        }
    }



    /**
     * 5.4待审核变更请求批复-----(完成)
     */
    @Override
    public void change(Integer request_id, String auditor_id, Integer auditor_result) {

        ChangeRequest_sql changeRequest_sql = new ChangeRequest_sql();
        //首判断是否管理员
        Admin adminByAccount = adminMapper.findAdminByAccount(auditor_id);
        String id = adminByAccount.getId();
        if (Objects.equals(id, auditor_id)) {
            //通过request_id找到company_id
            ChangeRequest_sql massage = changeReviewMapper.selectMesage(request_id);
            String company_id =massage.getCompany_id();
            //获取提交批复时间
            Timestamp submit_date = Timestamp.valueOf(LocalDateTime.now());
            changeRequest_sql.setSubmit_date(submit_date);

            changeReviewMapper.change(company_id, auditor_id, auditor_result,submit_date);
        }

    }


    /**
     * 5.5发起变更请求-------(完成)
     */
    @Override
    public void update(String company_id, Integer change_properties, String new_value, MultipartFile evidence_file) {
        ChangeRequest_sql changeRequest_sql = new ChangeRequest_sql();
        //设置提交时间
        Timestamp submit_date = Timestamp.valueOf(LocalDateTime.now());
        changeRequest_sql.setSubmit_date(submit_date);
        String uploadFile = FileUtils.uploadFile(evidence_file);
        //获取管理员的姓名和电话
        Enterprise_sql enterpriseInfoById = enterpriseMapper.getEnterpriseInfoById(company_id);
        String name = enterpriseInfoById.getContacts();
        String phoneNum = enterpriseInfoById.getPhone_num();
        log.info("dayin:{}{}{}",enterpriseInfoById,name, phoneNum);

        changeReviewMapper.insert(company_id, change_properties, new_value, submit_date, uploadFile, name, phoneNum);
    }

    /**
     * 5.6发起检测报告变更请求
     */
//    @Override
//    public void changePool(PoolChange poolchange) {
//        PoolChange_sql poolChange_sql = new PoolChange_sql();
//        //转化检测时间
//        String testData = poolchange.getTest_data();
//        poolChange_sql.setTest_data(Timestamp.valueOf(testData));
//
//        //其他业务
//        changeReviewMapper.insertPool(poolchange);

//        //插入检测报告业务
//        String companyId = poolchange.getCompany_id();
//        String testData2 = poolchange.getTest_data();
//        Enterprise_sql enterpriseInfoById = enterpriseMapper.getEnterpriseInfoById(companyId);
//        String name = enterpriseInfoById.getContacts();
//        //传送检测报告
//        MultipartFile reportFile = poolchange.getReport_file();
//        String uploadFile = FileUtils.uploadFile(reportFile);
//        changeReviewMapper.insertFile(companyId, testData2, name,uploadFile);


//    }
    @Override
    public void changePool(String company_id, Double lhq_top, Double lhq_bottom,
                           Double co_top, Double co_bottom, Double gas_top, Double gas_bottom,
                           String test_organization,String test_date, MultipartFile report_file) {
        PoolChange_sql poolChange_sql = new PoolChange_sql();
        //转化时间
        Timestamp submit_date = Timestamp.valueOf(test_date);
        poolChange_sql.setTest_data(submit_date);

        changeReviewMapper.insertPool(company_id, lhq_top, lhq_bottom, co_top, co_bottom, gas_top, gas_bottom, test_organization, submit_date);

        //插入检测报告业务
        Enterprise_sql enterpriseInfoById = enterpriseMapper.getEnterpriseInfoById(company_id);
        String name = enterpriseInfoById.getContacts();
        log.info("转换时间：{}", name);

        //传送检测报告
        String uploadFile = FileUtils.uploadFile(report_file);
        changeReviewMapper.insertFile(company_id, test_date, name,uploadFile);

    }


}
